<?php

namespace app\models;

/**
 * This is the model class for table "recommend".
 *
 * @property int $id
 * @property int $member_id
 * @property string $member_name
 * @property int $recommend_id 推荐人ID
 * @property string $recommend_name
 * @property int $recommend_source 推荐人来源  1 user 2 member
 * @property int $opertion_id 实现操作者
 * @property string $opertion_name
 * @property int $recommend_type 1 跟进 2 到访 3认筹 4认购 5 签约 6 回款 7 无效 0 待跟进
 * @property int $recommend_commission
 * @property int $commission_status 0 待订 1 未发放 2 已发放
 * @property string $remark
 * @property int $update_time
 * @property int $create_time
 */
class Recommend extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'recommend';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['member_id', 'member_name', 'recommend_id', 'recommend_name', 'recommend_source', 'opertion_name', 'update_time', 'create_time'], 'required'],
            [['member_id', 'recommend_id', 'recommend_source', 'opertion_id', 'recommend_type', 'recommend_commission', 'commission_status', 'update_time', 'create_time'], 'integer'],
            [['member_name', 'recommend_name', 'opertion_name'], 'string', 'max' => 64],
            [['remark'], 'string', 'max' => 128],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'member_id' => 'Member ID',
            'member_name' => 'Member Name',
            'recommend_id' => 'Recommend ID',
            'recommend_name' => 'Recommend Name',
            'recommend_source' => 'Recommend Source',
            'opertion_id' => 'Opertion ID',
            'opertion_name' => 'Opertion Name',
            'recommend_type' => 'Recommend Type',
            'recommend_commission' => 'Recommend Commission',
            'commission_status' => 'Commission Status',
            'remark' => 'Remark',
            'update_time' => 'Update Time',
            'create_time' => 'Create Time',
        ];
    }

    public static function getNewMemberCount($id)
    {
        $new_member_count = Recommend::find()->where(['recommend_id' => $id, 'recommend_type' => 8])->count();
        return $new_member_count;
    }

    public static function getValidCount($id)
    {
        $valid_count = Recommend::find()->where(['recommend_id' => $id, 'recommend_type' => [1, 2, 3, 4, 5, 6]])->count();
        return $valid_count;
    }

    public static function getNoValidCount($id)
    {
        $no_valid_count = Recommend::find()->where(['recommend_id' => $id, 'recommend_type' => 7])->count();
        return $no_valid_count;
    }

    public static function noovercommission($id)
    {
        $no_over_commission = Recommend::find()->where(['recommend_id' => $id, 'commission_status' => 1])->sum('recommend_commission');
        return $no_over_commission;
    }

    public static function overcommission($id)
    {
        $over_commission = Recommend::find()->where(['recommend_id' => $id, 'commission_status' => 2])->sum('recommend_commission');
        return $over_commission;
    }

    public function getMember()
    {
        return $this->hasOne(Member::className(), ['id' => 'member_id']);
    }

}
